package com.cd2cd.code_template.curd.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cd2cd.code_template.curd.service.CURDService;
import com.cd2cd.code_template.curd.service.CustomService;
import com.cd2cd.code_template.vo.BaseRes;
import com.cd2cd.code_template.vo.PageData;

import javax.annotation.Resource;
import java.util.Map;

public class CurdServiceAdapter<T> implements CustomService<T> {

    @Resource( name = "CURDService")
    private CURDService curdService;

    @Override
    public BaseRes<PageData<T>> entityPage(T entity, Map<String, Object> params) throws Exception {
        params.putAll(JSONObject.parseObject(JSONObject.toJSONString(entity), Map.class));
        return curdService.entityPage(entity.getClass().getSimpleName(), params);
    }

    @Override
    public BaseRes<T> entityInfo(T entity, Map<String, Object> params) throws Exception {
        params.putAll(JSONObject.parseObject(JSONObject.toJSONString(entity), Map.class));
        return curdService.entityInfo(entity.getClass().getSimpleName(), params);
    }

    @Override
    public BaseRes<T> entityUpdate(T entity, Map<String, Object> params) throws Exception {
        params.putAll(JSONObject.parseObject(JSONObject.toJSONString(entity), Map.class));
        return curdService.entityUpdate(entity.getClass().getSimpleName(), params);
    }

    @Override
    public BaseRes<T> entityAdd(T entity, Map<String, Object> params) throws Exception {
        params.putAll(JSONObject.parseObject(JSONObject.toJSONString(entity), Map.class));
        return curdService.entityAdd(entity.getClass().getSimpleName(), params);
    }

    @Override
    public BaseRes<T> entityDel(T entity, Map<String, Object> params) throws Exception {
        params.putAll(JSONObject.parseObject(JSONObject.toJSONString(entity), Map.class));
        return curdService.entityDel(entity.getClass().getSimpleName(), params);
    }

    @Override
    public BaseRes<Object> dynamicDic(String entity, Map<String, Object> params) throws Exception {
        params.putAll(JSONObject.parseObject(JSONObject.toJSONString(entity), Map.class));
        return curdService.dynamicDic(entity.getClass().getSimpleName(), params);
    }
}
